package org.fuyd.dao;

import java.sql.SQLException;
import java.util.List;

import org.fuyd.model.Log;
import org.fuyd.util.PageUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class LogDaoImpl implements LogDao {
  private HibernateTemplate hibernateTemplate;

  private SessionFactory sessionFactory;

  public void add(Log log) {
    hibernateTemplate.save(log);
  }

  public List<Log> findByPage(int page) {
    final int i = (page - 1) * 3;
    List<Log> logs = hibernateTemplate.execute(new HibernateCallback<List<Log>>() {

      @SuppressWarnings("unchecked")
      public List<Log> doInHibernate(Session session) throws HibernateException, SQLException {

        return (List<Log>) PageUtil.getList(session, "from Log l", i, 3);
      }
    });
    return logs;
  }

  public HibernateTemplate getHibernateTemplate() {
    return hibernateTemplate;
  }

  public SessionFactory getSessionFactory() {
    return sessionFactory;
  }

  public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    this.hibernateTemplate = hibernateTemplate;
  }

  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }

}
